<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      const data=JSON.parse(JSON.stringify(this.chartData))
      this.setOptions(data);
    },
    setOptions({ lastData, nowData } = {}) {
      console.log(lastData,nowData)
      this.chart.setOption({
        //X轴
        xAxis: {
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
          //坐标轴两边留白策略
          boundaryGap: false,
          //坐标轴刻度相关设置
          axisTick: {
            show: true
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          //是否有刻度标签
          containLabel: true
        },
        tooltip: {
          //触发类型
          trigger: "axis",
          //坐标轴指示器
          axisPointer: {
            type: "cross"
          },
          padding: [5, 10]
        },
        //Y轴
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ["Now", "Last"]
        },
        series: [
          {
            name: "Now",
            itemStyle: {
              normal: {
                color: "#FF005A",
                lineStyle: {
                  color: "#FF005A",
                  width: 2
                }
              }
            },
            smooth: true,
            type: "line",
            data: nowData,
            animationDuration: 2800,
            animationEasing: "cubicInOut"
          },
          {
            name: "Last",
            smooth: true,
            type: "line",
            itemStyle: {
              normal: {
                color: "#3888fa",
                lineStyle: {
                  color: "#3888fa",
                  width: 2
                },
                areaStyle: {
                  color: "#f3f8ff"
                }
              }
            },
            data: lastData,
            animationDuration: 2800,
            animationEasing: "quadraticOut"
          }
        ]
      });
    },
    //对象深复制，不考虑循环引用的情况
    cloneObj(from) {
      return Object.keys(from).reduce(
        (obj, key) => ((obj[key] = this.clone(from[key])), obj),
        {}
      );
    },
    //数组深复制，不考虑循环引用的情况
    cloneArr(from) {
      return from.map(n => this.clone(n));
    },
    // 复制输入值
    clone(from) {
      if (from instanceof Array) {
        return this.cloneArr(from);
      } else if (from instanceof Object) {
        return this.cloneObj(from);
      } else {
        return from;
      }
    }
  }
};
</script>
